package com.woman.beautylive.presentation.ui.main.currency;

import android.util.Log;

import com.woman.beautylive.data.bean.BaseResponse;
import com.woman.beautylive.data.bean.CurrencyRankItem;
import com.woman.beautylive.data.bean.PageBean;
import com.woman.beautylive.domain.CurrencyManager;
import com.woman.beautylive.presentation.ui.base.BaseObserver;
import com.woman.beautylive.presentation.ui.base.BasePresenter;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huanzhang on 2016/5/9.
 */
public class CurrencyPresenter extends BasePresenter{
    private ICurrency iCurrency;
    private CurrencyManager mManager;
    private int page = 0;
    protected CurrencyPresenter(ICurrency uiInterface) {
        super(uiInterface);
        iCurrency = uiInterface;
        mManager = new CurrencyManager();
    }

    public void getData(String uid){
        Subscription subscription = mManager.getCurrentByUser(uid,page+1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<PageBean<CurrencyRankItem>>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<PageBean<CurrencyRankItem>> response) {
                        if(response.getData()!= null){
                            if(response.getData().getList()!=null && response.getData().getList().size()!=0){
                               page++;
                            }
                        }
                        iCurrency.showData(response.getData());
                    }
                });
        addSubscription(subscription);
    }

    public void getRefreshData(String uid){
        Subscription subscription = mManager.getCurrentByUser(uid,1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<PageBean<CurrencyRankItem>>>(getUiInterface()) {
                    @Override
                    public void onSuccess(BaseResponse<PageBean<CurrencyRankItem>> response) {
                        if(response.getData()!= null){
                            if(response.getData().getList()!=null && response.getData().getList().size()!=0){
                                page=1;
                            }
                        }
                        iCurrency.showRefreshData(response.getData());
                    }
                });
        addSubscription(subscription);
    }
}
